package com.nutricia.core.service.impl;

import java.text.SimpleDateFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.nutricia.common.api.request.AddMeetingReq;
import com.nutricia.common.util.CheckUtil;
import com.nutricia.core.ex.BizErrorCode;
import com.nutricia.core.ex.BizException;
import com.nutricia.core.model.domain.MeetingDO;
import com.nutricia.core.model.domain.OptionDO;
import com.nutricia.core.service.MeetingService;

/**
 * 
* 会议service实现类
* @author wangzl
* @date 2018年4月25日
 */
@Service("MeetingService")
public class MeetingServiceImpl extends AbstractServiceImpl implements MeetingService{

	@Override
	@Transactional
	public String addMeeting(AddMeetingReq addMeetingReq) {
		logger.info("会议创建开始！");
		Integer meetingID = null;
		int meetingCount = meetingMapper.checkMeeting(addMeetingReq.getmName());
		logger.info("新增会议验证："+addMeetingReq.getmName()+"存在"+meetingCount+"条数据，可以新增");
		CheckUtil.isTrue(meetingCount == 0, BizErrorCode.MEETING_NAME_IS_EXIST, 
				BizErrorCode.MEETING_NAME_IS_EXIST.getDescription());
		//日期格式转换成字符串
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			MeetingDO meetingDO = new MeetingDO();
			meetingDO.setmName(addMeetingReq.getmName());
			meetingDO.setmStartDate(sdf.parse(addMeetingReq.getmStartDate()));
			meetingDO.setmEndDate(sdf.parse(addMeetingReq.getmEndDate()));
			meetingDO.setmHostCity(addMeetingReq.getmHostCity());
			meetingDO.setHotel(addMeetingReq.getHotel());
			meetingDO.setAddress(addMeetingReq.getAddress());
			meetingDO.setContacts(addMeetingReq.getContacts());
			meetingDO.setContactNumber(addMeetingReq.getContactNumber());
			meetingDO.setContactEmail(addMeetingReq.getContactEmail());
			meetingDO.setmNatures(addMeetingReq.getmNatures());
			meetingDO.setmBriefIntr(addMeetingReq.getmBriefIntr());
			meetingDO.setAuthCode(addMeetingReq.getAuthCode());
			meetingDO.setState('1');
			meetingDO.setFounderId("待获取");
			//新增会议
			meetingMapper.addMeeting(meetingDO);
			//获取新增后的会议ID
			meetingID = meetingMapper.getMeetingIdByName(meetingDO.getmName());
			//新增会议选项
			String options [] = null;
			if(addMeetingReq.getOptions() != null) {
				options = addMeetingReq.getOptions().split(",");
			}
			if(options != null) {
				for(int i=0; i<options.length; i++) {
					OptionDO optionDO = new OptionDO();
					optionDO.setmId(meetingID);
					optionDO.setOptionName(options[i]);
					optionDO.setState('1');
					optionDO.setFounderId("待获取");
					meetingMapper.addOption(optionDO);
				}
			}
			
		} catch (Exception e) {
			 logger.error("创建会议时异常：{MeetingServiceImpl.addMeeting}", e);
	         throw new BizException(BizErrorCode.SYSTEM_ERROR, "系统异常");
		}
		logger.info("会议创建成功！");
		return meetingID+"";
	}

}
